/*
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
// d_scan.c
//
// Portable C scan-level rasterization code, all pixel depths.

#define FIXED_RENDER

#include "r_local.h"

unsigned char	*r_turb_pbase, *r_turb_pdest;
fixed16_t		r_turb_s, r_turb_t, r_turb_sstep, r_turb_tstep;
int				*r_turb_turb;
int				r_turb_spancount;

void D_DrawTurbulent8Span (void);


/*
=============
D_WarpScreen

this performs a slight compression of the screen at the same time as
the sine warp, to keep the edges from wrapping
=============
*/
void D_WarpScreen (void)
{
	int		w, h;
	int		u,v, u2, v2;
	byte	*dest;
	int		*turb;
	int		*col;
	byte	**row;

	static int	cached_width, cached_height;
	static byte	*rowptr[1200+AMP2*2];
	static int	column[1600+AMP2*2];

	//
	// these are constant over resolutions, and can be saved
	//
	w = r_newrefdef.width;
	h = r_newrefdef.height;
	if (w != cached_width || h != cached_height)
	{
		cached_width = w;
		cached_height = h;
		for (v=0 ; v<h+AMP2*2 ; v++)
		{
			v2 = (int)((float)v/(h + AMP2 * 2) * r_refdef.vrect.height);
			rowptr[v] = r_warpbuffer + (WARP_WIDTH * v2);
		}

		for (u=0 ; u<w+AMP2*2 ; u++)
		{
			u2 = (int)((float)u/(w + AMP2 * 2) * r_refdef.vrect.width);
			column[u] = u2;
		}
	}

	turb = intsintable + ((int)(r_newrefdef.time*SPEED)&(CYCLE-1));
	dest = vid.buffer + r_newrefdef.y * vid.rowbytes + r_newrefdef.x;

	for (v=0 ; v<h ; v++, dest += vid.rowbytes)
	{
		col = &column[turb[v]];
		row = &rowptr[v];
		for (u=0 ; u<w ; u+=4)
		{
			dest[u+0] = row[turb[u+0]][col[u+0]];
			dest[u+1] = row[turb[u+1]][col[u+1]];
			dest[u+2] = row[turb[u+2]][col[u+2]];
			dest[u+3] = row[turb[u+3]][col[u+3]];
		}
	}
}


#if	!id386

/*
=============
D_DrawTurbulent8Span
=============
*/
void D_DrawTurbulent8Span (void)
{
	int		sturb, tturb;

	do
	{
		sturb = ((r_turb_s + r_turb_turb[(r_turb_t>>16)&(CYCLE-1)])>>16)&63;
		tturb = ((r_turb_t + r_turb_turb[(r_turb_s>>16)&(CYCLE-1)])>>16)&63;
		*r_turb_pdest++ = *(r_turb_pbase + (tturb<<6) + sturb);
		r_turb_s += r_turb_sstep;
		r_turb_t += r_turb_tstep;
	} while (--r_turb_spancount > 0);
}

#endif	// !id386


/*
=============
Turbulent8
=============
*/
void Turbulent8 (espan_t *pspan)
{
	int				count;
	fixed16_t		snext, tnext;
	float			sdivz, tdivz, zi, z, du, dv, spancountminus1;
	float			sdivz16stepu, tdivz16stepu, zi16stepu;

	r_turb_turb = sintable + ((int)(r_newrefdef.time*SPEED)&(CYCLE-1));

	r_turb_sstep = 0;	// keep compiler happy
	r_turb_tstep = 0;	// ditto

	r_turb_pbase = (unsigned char *)cacheblock;

	sdivz16stepu = d_sdivzstepu * 16;
	tdivz16stepu = d_tdivzstepu * 16;
	zi16stepu = d_zistepu * 16;

	do
	{
		r_turb_pdest = (unsigned char *)((byte *)d_viewbuffer +
				(r_screenwidth * pspan->v) + pspan->u);

		count = pspan->count;

	// calculate the initial s/z, t/z, 1/z, s, and t and clamp
		du = (float)pspan->u;
		dv = (float)pspan->v;

		sdivz = d_sdivzorigin + dv*d_sdivzstepv + du*d_sdivzstepu;
		tdivz = d_tdivzorigin + dv*d_tdivzstepv + du*d_tdivzstepu;
		zi = d_ziorigin + dv*d_zistepv + du*d_zistepu;
		z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point

		r_turb_s = (int)(sdivz * z) + sadjust;
		if (r_turb_s > bbextents)
			r_turb_s = bbextents;
		else if (r_turb_s < 0)
			r_turb_s = 0;

		r_turb_t = (int)(tdivz * z) + tadjust;
		if (r_turb_t > bbextentt)
			r_turb_t = bbextentt;
		else if (r_turb_t < 0)
			r_turb_t = 0;

		do
		{
		// calculate s and t at the far end of the span
			if (count >= 16)
				r_turb_spancount = 16;
			else
				r_turb_spancount = count;

			count -= r_turb_spancount;

			if (count)
			{
			// calculate s/z, t/z, zi->fixed s and t at far end of span,
			// calculate s and t steps across span by shifting
				sdivz += sdivz16stepu;
				tdivz += tdivz16stepu;
				zi += zi16stepu;
				z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point

				snext = (int)(sdivz * z) + sadjust;
				if (snext > bbextents)
					snext = bbextents;
				else if (snext < 16)
					snext = 16;	// prevent round-off error on <0 steps from
								//  from causing overstepping & running off the
								//  edge of the texture

				tnext = (int)(tdivz * z) + tadjust;
				if (tnext > bbextentt)
					tnext = bbextentt;
				else if (tnext < 16)
					tnext = 16;	// guard against round-off error on <0 steps

				r_turb_sstep = (snext - r_turb_s) >> 4;
				r_turb_tstep = (tnext - r_turb_t) >> 4;
			}
			else
			{
			// calculate s/z, t/z, zi->fixed s and t at last pixel in span (so
			// can't step off polygon), clamp, calculate s and t steps across
			// span by division, biasing steps low so we don't run off the
			// texture
				spancountminus1 = (float)(r_turb_spancount - 1);
				sdivz += d_sdivzstepu * spancountminus1;
				tdivz += d_tdivzstepu * spancountminus1;
				zi += d_zistepu * spancountminus1;
				z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
				snext = (int)(sdivz * z) + sadjust;
				if (snext > bbextents)
					snext = bbextents;
				else if (snext < 16)
					snext = 16;	// prevent round-off error on <0 steps from
								//  from causing overstepping & running off the
								//  edge of the texture

				tnext = (int)(tdivz * z) + tadjust;
				if (tnext > bbextentt)
					tnext = bbextentt;
				else if (tnext < 16)
					tnext = 16;	// guard against round-off error on <0 steps

				if (r_turb_spancount > 1)
				{
					r_turb_sstep = (snext - r_turb_s) / (r_turb_spancount - 1);
					r_turb_tstep = (tnext - r_turb_t) / (r_turb_spancount - 1);
				}
			}

			r_turb_s = r_turb_s & ((CYCLE<<16)-1);
			r_turb_t = r_turb_t & ((CYCLE<<16)-1);

			D_DrawTurbulent8Span ();

			r_turb_s = snext;
			r_turb_t = tnext;

		} while (count > 0);

	} while ((pspan = pspan->pnext) != NULL);
}

//====================
//PGM
/*
=============
NonTurbulent8 - this is for drawing scrolling textures. they're warping water textures
	but the turbulence is automatically 0.
=============
*/
void NonTurbulent8 (espan_t *pspan)
{
	int				count;
	fixed16_t		snext, tnext;
	float			sdivz, tdivz, zi, z, du, dv, spancountminus1;
	float			sdivz16stepu, tdivz16stepu, zi16stepu;

//	r_turb_turb = sintable + ((int)(r_newrefdef.time*SPEED)&(CYCLE-1));
	r_turb_turb = blanktable;

	r_turb_sstep = 0;	// keep compiler happy
	r_turb_tstep = 0;	// ditto

	r_turb_pbase = (unsigned char *)cacheblock;

	sdivz16stepu = d_sdivzstepu * 16;
	tdivz16stepu = d_tdivzstepu * 16;
	zi16stepu = d_zistepu * 16;

	do
	{
		r_turb_pdest = (unsigned char *)((byte *)d_viewbuffer +
				(r_screenwidth * pspan->v) + pspan->u);

		count = pspan->count;

	// calculate the initial s/z, t/z, 1/z, s, and t and clamp
		du = (float)pspan->u;
		dv = (float)pspan->v;

		sdivz = d_sdivzorigin + dv*d_sdivzstepv + du*d_sdivzstepu;
		tdivz = d_tdivzorigin + dv*d_tdivzstepv + du*d_tdivzstepu;
		zi = d_ziorigin + dv*d_zistepv + du*d_zistepu;
		z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point

		r_turb_s = (int)(sdivz * z) + sadjust;
		if (r_turb_s > bbextents)
			r_turb_s = bbextents;
		else if (r_turb_s < 0)
			r_turb_s = 0;

		r_turb_t = (int)(tdivz * z) + tadjust;
		if (r_turb_t > bbextentt)
			r_turb_t = bbextentt;
		else if (r_turb_t < 0)
			r_turb_t = 0;

		do
		{
		// calculate s and t at the far end of the span
			if (count >= 16)
				r_turb_spancount = 16;
			else
				r_turb_spancount = count;

			count -= r_turb_spancount;

			if (count)
			{
			// calculate s/z, t/z, zi->fixed s and t at far end of span,
			// calculate s and t steps across span by shifting
				sdivz += sdivz16stepu;
				tdivz += tdivz16stepu;
				zi += zi16stepu;
				z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point

				snext = (int)(sdivz * z) + sadjust;
				if (snext > bbextents)
					snext = bbextents;
				else if (snext < 16)
					snext = 16;	// prevent round-off error on <0 steps from
								//  from causing overstepping & running off the
								//  edge of the texture

				tnext = (int)(tdivz * z) + tadjust;
				if (tnext > bbextentt)
					tnext = bbextentt;
				else if (tnext < 16)
					tnext = 16;	// guard against round-off error on <0 steps

				r_turb_sstep = (snext - r_turb_s) >> 4;
				r_turb_tstep = (tnext - r_turb_t) >> 4;
			}
			else
			{
			// calculate s/z, t/z, zi->fixed s and t at last pixel in span (so
			// can't step off polygon), clamp, calculate s and t steps across
			// span by division, biasing steps low so we don't run off the
			// texture
				spancountminus1 = (float)(r_turb_spancount - 1);
				sdivz += d_sdivzstepu * spancountminus1;
				tdivz += d_tdivzstepu * spancountminus1;
				zi += d_zistepu * spancountminus1;
				z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
				snext = (int)(sdivz * z) + sadjust;
				if (snext > bbextents)
					snext = bbextents;
				else if (snext < 16)
					snext = 16;	// prevent round-off error on <0 steps from
								//  from causing overstepping & running off the
								//  edge of the texture

				tnext = (int)(tdivz * z) + tadjust;
				if (tnext > bbextentt)
					tnext = bbextentt;
				else if (tnext < 16)
					tnext = 16;	// guard against round-off error on <0 steps

				if (r_turb_spancount > 1)
				{
					r_turb_sstep = (snext - r_turb_s) / (r_turb_spancount - 1);
					r_turb_tstep = (tnext - r_turb_t) / (r_turb_spancount - 1);
				}
			}

			r_turb_s = r_turb_s & ((CYCLE<<16)-1);
			r_turb_t = r_turb_t & ((CYCLE<<16)-1);

			D_DrawTurbulent8Span ();

			r_turb_s = snext;
			r_turb_t = tnext;

		} while (count > 0);

	} while ((pspan = pspan->pnext) != NULL);
}
//PGM
//====================


#if	!id386



//#define SUBDIVIDE 256
//#define DIVIDESHIFT 8

// 8 pixel div
//#define DIVIDESHIFT 3

// 16 pixel div
#define DIVIDESHIFT 4

// 32 pixel div
//#define DIVIDESHIFT 5
#define SUBDIVIDE (1 << (DIVIDESHIFT))

//#define SUBDIVIDE 8

/*
=============
D_DrawSpans16

  FIXME: actually make this subdivide by 16 instead of 8!!!
=============
*/

#ifdef GCCBROKENRELATIVEPATHS
#include "../../riscos/LogFloat.h"
#else
#include "../riscos/LogFloat.h"
#endif

#ifndef FIXED_RENDER
void D_DrawSpans16 (espan_t *pspan)
{
	int				count, spancount;
	unsigned char	*pbase, *pdest;
	fixed16_t		s, t, snext, tnext, sstep, tstep;
	float			sdivz, tdivz, zi, z, du, dv, spancountminus1;
	float			sdivz8stepu, tdivz8stepu, zi8stepu;

	sstep = 0;	// keep compiler happy
	tstep = 0;	// ditto

	pbase = (unsigned char *)cacheblock;

//LogFloat(d_sdivzorigin, "d_sdivzorigin", -1, -1);
//LogFloat(d_tdivzorigin, "d_tdivzorigin", -1, -1);
//LogFloat(d_sdivzstepv,  "d_sdivzstepv", -1, -1);
//LogFloat(d_tdivzstepv,  "d_tdivzstepv", -1, -1);
//LogFloat(d_sdivzstepu,  "d_sdivzstepu", -1, -1);
//LogFloat(d_tdivzstepu,  "d_tdivzstepu", -1, -1);
//LogFloat(d_ziorigin,    "d_ziorigin", -1, -1);
//LogFloat(d_zistepv ,    "d_zistepv ", -1, -1);
//LogFloat(d_zistepu ,    "d_zistepu ", -1, -1);

	sdivz8stepu = d_sdivzstepu * SUBDIVIDE;
	tdivz8stepu = d_tdivzstepu * SUBDIVIDE;
	zi8stepu = d_zistepu * SUBDIVIDE;

	do
	{
		pdest = (unsigned char *)((byte *)d_viewbuffer +
				(r_screenwidth * pspan->v) + pspan->u);

		count = pspan->count;

	// calculate the initial s/z, t/z, 1/z, s, and t and clamp
		du = (float)pspan->u;
		dv = (float)pspan->v;

		sdivz = d_sdivzorigin + dv*d_sdivzstepv + du*d_sdivzstepu;
		tdivz = d_tdivzorigin + dv*d_tdivzstepv + du*d_tdivzstepu;
//LogFloat(sdivz,    "sdivz", -1, -1);
//LogFloat(tdivz,    "tdivz", -1, -1);
		zi = d_ziorigin + dv*d_zistepv + du*d_zistepu;
		z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point

		s = (int)(sdivz * z) + sadjust;
		if (s > bbextents)
			s = bbextents;
		else if (s < 0)
			s = 0;

		t = (int)(tdivz * z) + tadjust;
		if (t > bbextentt)
			t = bbextentt;
		else if (t < 0)
			t = 0;

		do
		{
		// calculate s and t at the far end of the span
			if (count >= SUBDIVIDE)
				spancount = SUBDIVIDE;
			else
				spancount = count;

			count -= spancount;

			if (count)
			{
			// calculate s/z, t/z, zi->fixed s and t at far end of span,
			// calculate s and t steps across span by shifting
				sdivz += sdivz8stepu;
				tdivz += tdivz8stepu;
				zi += zi8stepu;
				z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point

				snext = (int)(sdivz * z) + sadjust;
				if (snext > bbextents)
					snext = bbextents;
				else if (snext < SUBDIVIDE)
					snext = SUBDIVIDE;	// prevent round-off error on <0 steps from
								//  from causing overstepping & running off the
								//  edge of the texture

				tnext = (int)(tdivz * z) + tadjust;
				if (tnext > bbextentt)
					tnext = bbextentt;
				else if (tnext < SUBDIVIDE)
					tnext = SUBDIVIDE;	// guard against round-off error on <0 steps
// >>3 = /8?
				sstep = (snext - s) >> DIVIDESHIFT;
				tstep = (tnext - t) >> DIVIDESHIFT;
			}
			else
			{
			// calculate s/z, t/z, zi->fixed s and t at last pixel in span (so
			// can't step off polygon), clamp, calculate s and t steps across
			// span by division, biasing steps low so we don't run off the
			// texture
				spancountminus1 = (float)(spancount - 1);
				sdivz += d_sdivzstepu * spancountminus1;
				tdivz += d_tdivzstepu * spancountminus1;
				zi += d_zistepu * spancountminus1;
				z = (float)0x10000 / zi;	// prescale to 16.16 fixed-point
				snext = (int)(sdivz * z) + sadjust;
				if (snext > bbextents)
					snext = bbextents;
				else if (snext < SUBDIVIDE)
					snext = SUBDIVIDE;	// prevent round-off error on <0 steps from
								//  from causing overstepping & running off the
								//  edge of the texture

				tnext = (int)(tdivz * z) + tadjust;
				if (tnext > bbextentt)
					tnext = bbextentt;
				else if (tnext < SUBDIVIDE)
					tnext = SUBDIVIDE;	// guard against round-off error on <0 steps

				if (spancount > 1)
				{
					sstep = (snext - s) / (spancount - 1);
					tstep = (tnext - t) / (spancount - 1);
				}
			}

			do
			{
				*pdest++ = *(pbase + (s >> 16) + (t >> 16) * cachewidth);
				s += sstep;
				t += tstep;
			} while (--spancount > 0);

			s = snext;
			t = tnext;

		} while (count > 0);

	} while ((pspan = pspan->pnext) != NULL);
}
#endif // #ifndef FIXED_RENDER

#endif


#if	!id386

/*
=============
D_DrawZSpans
==
y===========
*/
#ifndef FIXED_RENDER
void D_DrawZSpans (espan_t *pspan)
{
	int				count, doublecount, izistep;
	int				izi;
	short			*pdest;
	unsigned		ltemp;
	float			zi;
	float			du, dv;

// FIXME: check for clamping/range problems
// we count on FP exceptions being turned off to avoid range problems
	izistep = (int)(d_zistepu * 0x8000 * 0x10000);

	do
	{
		pdest = d_pzbuffer + (d_zwidth * pspan->v) + pspan->u;

		count = pspan->count;

	// calculate the initial 1/z
		du = (float)pspan->u;
		dv = (float)pspan->v;

		zi = d_ziorigin + dv*d_zistepv + du*d_zistepu;
	// we count on FP exceptions being turned off to avoid range problems
		izi = (int)(zi * 0x8000 * 0x10000);

		if ((long)pdest & 0x02)
		{
			*pdest++ = (short)(izi >> 16);
			izi += izistep;
			count--;
		}

		if ((doublecount = count >> 1) > 0)
		{
			do
			{
				ltemp = izi >> 16;
				izi += izistep;
				ltemp |= izi & 0xFFFF0000;
				izi += izistep;
				*(int *)pdest = ltemp;
				pdest += 2;
			} while (--doublecount > 0);
		}

		if (count & 1)
			*pdest = (short)(izi >> 16);

	} while ((pspan = pspan->pnext) != NULL);
}
#endif // #ifndef FIXED_RENDER

#endif

#ifdef FIXED_RENDER
static int d_zistepu_fxp, d_zistepv_fxp, d_ziorigin_fxp;

static int sdivzorig, sdivzstepv, sdivzstepu, sdivz8stepu;
static int tdivzorig, tdivzstepv, tdivzstepu, tdivz8stepu;
static int zi8stepu;
//static float last = 0;

void UpdateFixedPointVars( int all )
{
	// JB: Store texture transformation matrix in fixed point vars
	if (all)
	{
#ifdef RISCOS_FPLIB
		sdivzorig   = FIX_FLOAT_MEM_SL(&d_sdivzorigin, 22); // 10.22 fixed point
		tdivzorig   = FIX_FLOAT_MEM_SL(&d_tdivzorigin, 22);
		sdivzstepv  = FIX_FLOAT_MEM_SL(&d_sdivzstepv,  22);
		tdivzstepv  = FIX_FLOAT_MEM_SL(&d_tdivzstepv,  22);
		sdivzstepu  = FIX_FLOAT_MEM_SL(&d_sdivzstepu,  22);
		tdivzstepu  = FIX_FLOAT_MEM_SL(&d_tdivzstepu, 22);
		sdivz8stepu = sdivzstepu << DIVIDESHIFT;
		tdivz8stepu = tdivzstepu << DIVIDESHIFT;
#else
		sdivzorig = (int)(4194304.0f * d_sdivzorigin); // 10.22 fixed point
		tdivzorig = (int)(4194304.0f * d_tdivzorigin);
		sdivzstepv = (int)(4194304.0f * d_sdivzstepv);
		tdivzstepv = (int)(4194304.0f * d_tdivzstepv);
		sdivzstepu = (int)(4194304.0f * d_sdivzstepu);
		tdivzstepu = (int)(4194304.0f * d_tdivzstepu);
		sdivz8stepu = sdivzstepu << DIVIDESHIFT;
		tdivz8stepu = tdivzstepu << DIVIDESHIFT;
#endif
	}

#ifdef RISCOS_FPLIB
	d_ziorigin_fxp = FIX_FLOAT_MEM_SL(&d_ziorigin, 22);  // 10.22 fixed point
	d_zistepv_fxp  = FIX_FLOAT_MEM_SL(&d_zistepv,  22);
	d_zistepu_fxp  = FIX_FLOAT_MEM_SL(&d_zistepu,  22);
#else
	d_ziorigin_fxp = (int)(4194304.0f * d_ziorigin);  // 10.22 fixed point
	d_zistepv_fxp = (int)(4194304.0f * d_zistepv );
	d_zistepu_fxp = (int)(4194304.0f * d_zistepu );
#endif
	zi8stepu = d_zistepu_fxp << DIVIDESHIFT;
//	last = d_zistepv;
}

void D_DrawSpans16 (espan_t *pspan)
{
    int count, spancount, spancountminus1;
    unsigned char *pbase, *pdest;
    fixed16_t s1, t1;
    int zi, sdivz, tdivz, sstep, tstep;
    int snext, tnext;
    pbase = (unsigned char *)cacheblock;
    //Jacco Biker's fixed point conversion

    // Recalc fixed point values
    UpdateFixedPointVars( 1 );
    do
    {
        pdest = (unsigned char *)((byte *)d_viewbuffer + (r_screenwidth * pspan->v) + pspan->u);
        count = pspan->count;
        // calculate the initial s/z, t/z, 1/z, s, and t and clamp
        sdivz = sdivzorig + pspan->v * sdivzstepv + pspan->u * sdivzstepu;
        tdivz = tdivzorig + pspan->v * tdivzstepv + pspan->u * tdivzstepu;
        zi = d_ziorigin_fxp + pspan->v * d_zistepv_fxp + pspan->u * d_zistepu_fxp;
        if (!zi) zi = 1;

        // Calculate initial (left hand pixel) perspective correction
        s1 = (((sdivz << 7) / zi) << 9) + sadjust;    // 5.27 / 13.19 = 24.8 >> 8 = 16.16
        if (s1 > bbextents) s1 = bbextents;
        else if (s1 < 0)    s1 = 0;

        t1 = (((tdivz << 7) / zi) << 9) + tadjust;
        if (t1 > bbextentt) t1 = bbextentt;
        else if (t1 < 0)    t1 = 0;

        //End Jacco Biker mod
        //Dan East: Fixed point conversion for perspective correction
        do
        {
            // calculate s and t at the far end of the span
            spancount  = (count >= SUBDIVIDE) ? SUBDIVIDE : count;
            count     -= spancount;

            if (count)
            {
                // calculate s/z, t/z, zi->fixed s and t at far end of span,
                // calculate s and t steps across span by shifting
                sdivz += sdivz8stepu;
                tdivz += tdivz8stepu;
                zi    += zi8stepu;
                if (!zi) zi = 1; // Make sure we never divide by zero
                //z = zi;
                //z = (float)0x10000 / zi;    // prescale to 16.16 fixed-point
                snext = (((sdivz << 7) / zi) << 9) + sadjust; // Losing precision here

                if      (snext > bbextents) snext = bbextents;
                else if (snext < SUBDIVIDE) snext = SUBDIVIDE; // prevent round-off error on <0 steps from from causing
                                                               // overstepping & running off the edge of the texture

                tnext = (((tdivz << 7) / zi) << 9) + tadjust; // Losing precision here

                if      (tnext > bbextentt) tnext = bbextentt;
                else if (tnext < SUBDIVIDE) tnext = SUBDIVIDE;    // guard against round-off error on <0 steps

                sstep = (snext - s1) >> DIVIDESHIFT;
                tstep = (tnext - t1) >> DIVIDESHIFT;
            }
            else
            {
                // calculate s/z, t/z, zi->fixed s and t at last pixel in span (so
                // can't step off polygon), clamp, calculate s and t steps across
                // span by division, biasing steps low so we don't run off the
                // texture
                spancountminus1 = spancount - 1;
                sdivz += sdivzstepu * spancountminus1;
                tdivz += tdivzstepu * spancountminus1;
                zi += d_zistepu_fxp * spancountminus1;
                if (!zi) zi = 1; // Make sure we never divide by zero

                snext = (((sdivz<<7) / zi)<<9) + sadjust;

                if (snext > bbextents)      snext = bbextents;
                else if (snext < SUBDIVIDE) snext = SUBDIVIDE;    // prevent round-off error on <0 steps from
                                                                  //  from causing overstepping & running off the
                                                                  //  edge of the texture

                tnext = (((tdivz<<7)/zi)<<9) + tadjust;

                if (tnext > bbextentt)      tnext = bbextentt;
                else if (tnext < SUBDIVIDE) tnext = SUBDIVIDE;    // guard against round-off error on <0 steps

                if (spancount > 1)
                {
                    sstep = ((snext - s1)) / ((spancount - 1));
                    tstep = ((tnext - t1)) / ((spancount - 1));
                }
            }

            do
            {
                *pdest++ = *(pbase + (s1 >> 16) + (t1 >> 16) * cachewidth);
                s1 += sstep;
                t1 += tstep;
            } while (--spancount);

            s1 = snext;
            t1 = tnext;
        } while (count > 0);
    } while ((pspan = pspan->pnext) != NULL);
}

void D_DrawZSpans (espan_t *pspan)
{
	int count, doublecount, izistep;
	int izi;
	short *pdest;
	unsigned ltemp;
#ifdef USE_PQ_OPT5
	// Recalc fixed point values
	UpdateFixedPointVars( 0 );
#endif
	izistep = d_zistepu_fxp << 9;

	do
	{
		pdest = d_pzbuffer + (d_zwidth * pspan->v) + pspan->u;
		count = pspan->count;
		// calculate the initial 1/z
		izi = (d_ziorigin_fxp + pspan->v * d_zistepv_fxp + pspan->u * d_zistepu_fxp) << 9; // 1.31 fixed point

		if ((long)pdest & 0x02)
		{
			// Write first z element to round up to word alignment
			*pdest++ = (short)(izi >> 16);
			izi += izistep;
			count--;
		}
		if ((doublecount = count >> 1) > 0)
		{
			do
			{
				ltemp = izi >> 16;
				izi += izistep;
				ltemp |= izi & 0xFFFF0000;
				izi += izistep;
				*(int *)pdest = ltemp;
				pdest += 2;
			} while (--doublecount > 0);
		}
		if (count & 1) *pdest = (short)(izi >> 16);

	} while ((pspan = pspan->pnext) != NULL);
}
#endif
